<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import type { FormRules } from "element-plus";
import ReCol from "@/components/ReCol";
import { FormProps } from "./types";
import { usePublicHooks } from "@/views/hooks";
import { CategoryListTree } from "@/api/pms/catogory";
const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    higherOptions: [], //状态其他数据,
    dictOptionData: {}, //字典数据
    id: null,
    name: null,
    cid: null,
    sort: null,
    revision: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    delFlag: null,
    tenantId: null
  })
});

const ruleFormRef = ref();
const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);
const showValueType = ref(false);
const categotyList = ref([]);

console.log("newFormInline", newFormInline);
newFormInline.value.cid = parseInt(newFormInline.value.cid);
console.log("newFormInline", newFormInline.value);
function getRef() {
  return ruleFormRef.value;
}
//校验规则
const formRules = reactive(<FormRules>{
  name: [{ required: true, message: "名称为必填项", trigger: "blur" }],
  cid: [{ required: true, message: "请选择分类", trigger: "blur" }],
  remark: []
});

function changeCid(params: any) {
  console.log("params------>", params);
}

onMounted(() => {});

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="分类" prop="cid">
          <el-cascader
            v-model="newFormInline.cid"
            class="w-full"
            :options="newFormInline.dictOptionData.categotyList"
            :props="{
              value: 'id',
              label: 'name',
              children: 'children',
              emitPath: false,
              checkStrictly: false
            }"
            clearable
            filterable
            placeholder="请选择上级分类"
            @change="changeCid"
          >
            <template #default="{ node, data }">
              <span>{{ data.name }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>
        </el-form-item>

        <el-form-item label="分组名" prop="name">
          <el-input
            v-model="newFormInline.name"
            clearable
            placeholder="请输入分组名"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input
            v-model="newFormInline.sort"
            type="number"
            clearable
            placeholder="请输入排序"
          />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="newFormInline.remark"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            clearable
            placeholder="请输入备注"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
